<?php

/*
 * -------------------------------------
 * Angello Ferley Rios Arredondo
 * framework mvc basico
 * View.php
 * -------------------------------------
 */


class View
{
    private $_controlador;
    private $_js;
    private $_cssSig;
    
    public function __construct(Request $peticion) {
        $this->_controlador = $peticion->getControlador();
        $this->_js = array();    
        $this->_cssSig = array();
    }
    
    public function renderizar($vista, $item = false)
    {
        $menu = array(
            array(
                'id' => 'inicio',
                'titulo' => 'Inicio',
                'enlace' => BASE_URL)
        );
		
		$menuSig = array(
            
        		array(
        				'id' => 'sig',
        				'titulo' => 'sig Campus',
        				'enlace' => BASE_URL . 'sigCampus'
        		),
        		array(
        				'id' => 'posts',
        				'titulo' => 'posts',
        				'enlace' => BASE_URL . 'posts'
        		)
        );
		
        if(Session::get('autenticado'))
		{
			$menu[]= array(
        				'id' => 'login',
        				'titulo' => 'cerrar Sesion',
        				'enlace' => BASE_URL . 'login/cerrar'
        		);
		}else{
			$menu[]= array(
        				'id' => 'login',
        				'titulo' => 'Iniciar Sesion',
        				'enlace' => BASE_URL . 'login'
        		);
				
			$menu[]= array(
        				'id' => 'registro',
        				'titulo' => 'Registro',
        				'enlace' => BASE_URL . 'registro'
        		);
				
		}
	
        $js = array();
        $cssSig = array();
        
        if(count($this->_js)){
        	$js = $this->_js;
        }
        
        if(count($this->_cssSig)){
        	$cssSig = $this->_cssSig;
        }
        
        $_layoutParams = array(
            'ruta_css' => BASE_URL . 'views/layout/' . DEFAULT_LAYOUT . '/css/',
            'ruta_img' => BASE_URL . 'views/layout/' . DEFAULT_LAYOUT . '/img/',
            'ruta_js' => BASE_URL . 'views/layout/' . DEFAULT_LAYOUT . '/js/',
            'menu' => $menu,
            'menuSig' => $menuSig,
        	'js' => $js,
        	'cssSig' => $cssSig		
        );
        
        $rutaView = ROOT . 'views' . DS . $this->_controlador . DS . $vista . '.phtml';
        if(Session::get('autenticado'))
        {
        if(is_readable($rutaView))
        {
            include_once ROOT . 'views'. DS . 'layout' . DS . DEFAULT_LAYOUT . DS . 'header.php';
            include_once $rutaView;
            include_once ROOT . 'views'. DS . 'layout' . DS . DEFAULT_LAYOUT . DS . 'footer.php';
        } 
        }else if (is_readable($rutaView)) 
        {
        	include_once $rutaView;
        }
        else {
            throw new Exception('Error de vista');
        }
    }
    
    public function setJs(Array $js)
    {
    	
    	if(is_array($js) && count($js))
    	{
    		for ($i=0;$i<count($js);$i++)
    		{
    			$this->_js[] = BASE_URL. 'views/'. $this->_controlador. '/js/'. $js[$i].'.js';
    		}
    	}else
    	{
    		throw new Exception('Error de js');
    	}
    	
    }
    
    public function setCss(Array $cssSig)
    {	 
    	if(is_array($cssSig) && count($cssSig))
    	{
    		for ($i=0;$i<count($cssSig);$i++)
    		{
    		$this->_cssSig[] = BASE_URL. 'views/'. $this->_controlador. '/css/'. $cssSig[$i].'.css';
    		}
    		}else
    		{
    		throw new Exception('Error de css');
    		}  		 
    }  
}

?>
